﻿using System;
using System.Windows;
using System.Windows.Navigation;
using Microsoft.Phone.Controls;
using Microsoft.Phone.Shell;
using Notes.Model;
using System.Windows.Media;
using Microsoft.Phone.Tasks;
using System.Windows.Media.Imaging;
using Microsoft.Phone;
using System.IO.IsolatedStorage;
using System.Windows.Controls;
using System.Collections.ObjectModel;
using System.Linq;
using System.IO;

namespace Notes
{
    public partial class EditPage : PhoneApplicationPage
    {
        private PhotoChooserTask photo = new PhotoChooserTask();
        private int _NoteIndex;

        // Constructor
        public EditPage()
        {
            InitializeComponent();

            #region ApplicationBar
            // set applicatiobar button names
            ApplicationBar.BackgroundColor = (Color)Resources["PhoneBackgroundColor"];
            ApplicationBarIconButton btnImage = (ApplicationBarIconButton)ApplicationBar.Buttons[0];
            btnImage.Text = AppResources.strBarImage;
            ApplicationBarIconButton btnDel = (ApplicationBarIconButton)ApplicationBar.Buttons[1];
            btnDel.Text = AppResources.strBarDel;
            #endregion

            DataContext = App.EditViewModel;
            Loaded +=new RoutedEventHandler(EditPage_Loaded);
        }

        private void EditPage_Loaded(Object sender, RoutedEventArgs e)
        {

            // set photochooser
            photo.ShowCamera = true;
            photo.Completed += new EventHandler<PhotoResult>(ImageAdd_Completed);

            // select note
            string selectedIndex = null;
            if (NavigationContext.QueryString.TryGetValue("selectedIndex", out selectedIndex))
            {
                this._NoteIndex = int.Parse(selectedIndex);
                App.EditViewModel.LoadNote(_NoteIndex);
            }
            else if (App.EditViewModel.Note.Id > 0)
            { 
                
            }
            else
            {
                App.EditViewModel.LoadNote();
            }

        }

        private void ImageAdd_Completed(object sender, PhotoResult e)
        {
            if (e.TaskResult == TaskResult.OK)
            {
                string OriginalName = e.OriginalFileName;
                string[] NameArray = OriginalName.Split('\\');
                string Name = NameArray[NameArray.Length - 1];

                // store in collection
                Photo photo = new Photo();
                photo.FileName = Name;
                photo.Bitmap = new BitmapImage();
                photo.Bitmap.SetSource(e.ChosenPhoto);
                photo.NoteId = App.EditViewModel.Note.Id;
                App.EditViewModel.AddPhoto(photo);
            }
        }

        protected override void OnNavigatedFrom(NavigationEventArgs e)
        {
            base.OnNavigatedFrom(e);

            if (ContentTitle.Text.Length > 0 || ContentTitle.Text.Length > 0 || App.EditViewModel.Photos.Count > 0)
            {
                Note note = new Note();
                if (String.IsNullOrEmpty(ContentTitle.Text) || String.IsNullOrWhiteSpace(ContentTitle.Text))
                    note.Title = "Untitled";
                else
                    note.Title = ContentTitle.Text;
                note.Text = ContentText.Text;
                App.EditViewModel.SaveNote(note);
            }
        }

        #region ApplicationBar events

        private void ApplicationBarDel_Click(object sender, EventArgs e)
        {
            App.EditViewModel.DeleteNote();
        }

        private void ApplicationBarImage_Click(object sender, EventArgs e)
        {
            App.EditViewModel.IsProgressVisible = true;
            photo.Show();
        }
        #endregion

        private void PhoneApplicationPage_OrientationChanged(object sender, OrientationChangedEventArgs e)
        {
            if ((e.Orientation & PageOrientation.Portrait) == (PageOrientation.Portrait))
            {
                LayoutRoot.Margin = new Thickness(0, 32, 0, 72);
            }
            else
            {
                LayoutRoot.Margin = new Thickness(72, 0, 72, 0);
            }
        }

        private void ImagesListBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            if (ImagesListBox.SelectedIndex == -1) return;
            NavigationService.Navigate(new Uri("/PicturePage.xaml?selectedItem=" + ImagesListBox.SelectedIndex, UriKind.Relative));
            ImagesListBox.SelectedIndex = -1;
        }

    }
}